package com.vm.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONObject;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.vm.commons.VMConst;
import com.vm.db.UpgradeSoftDao;
import com.vm.model.AppInfo;
import com.vm.model.Soft;
import com.vm.model.UpgradeSoft;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年4月3日 上午9:51:15]
 * @Version: [v1.0]
 * 
 */
public class UpgradeUtil {

	public UpgradeUtil(Context context) {
		this.context = context;
		mQueue = MyVolley.getRequestQueue();
		dao = new UpgradeSoftDao(context);
	}

	RequestQueue mQueue;
	UpgradeSoftDao dao;

	Context context;

	int newVersion;
	int now = 0;
	int all = 0;

	public void getNew() {

		new Thread() {

			public void run() {
				perform();
			}

		}.start();

	}

	public void perform() {
		List<PackageInfo> packages = context.getPackageManager()
				.getInstalledPackages(0);

		for (int i = 0; i < packages.size(); i++) {

			final PackageInfo packageInfo = packages.get(i);
			AppInfo tmpInfo = new AppInfo();
			tmpInfo.appName = packageInfo.applicationInfo.loadLabel(
					context.getPackageManager()).toString();
			tmpInfo.packageName = packageInfo.packageName;
			tmpInfo.versionName = packageInfo.versionName;
			tmpInfo.versionCode = packageInfo.versionCode;

			tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(context
					.getPackageManager());
			if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {

				// tmpInfo.appIcon.toString();
				final UpgradeSoft upgradeSoft = new UpgradeSoft();

				if (!dao.ifExists(packageInfo.packageName)) {
					all++;
					String url = VMConst.REQUEST_URL
							+ "compareApk?packageName="
							+ packageInfo.packageName + "&version="
							+ packageInfo.versionName;

					JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
							url, null, new Listener<JSONObject>() {

								@Override
								public void onResponse(JSONObject arg0) {
									now += 1;

									System.out.println("now=" + now);
									Soft soft = JsonUtils.jsonToBean(arg0,
											Soft.class);
									if (soft != null) {
										newVersion++;
										System.out.println(upgradeSoft
												.getName()
												+ ",old:"
												+ upgradeSoft
														.getCurrentVersion()
												+ ",new:" + soft.getVersion());
										upgradeSoft.setId(UUID.randomUUID()
												.toString());
										upgradeSoft.setName(soft.getName());
										upgradeSoft.setPackageName(soft
												.getPackageName());
										upgradeSoft.setSize(soft.getSize());
										upgradeSoft.setSoftId(soft.getId());
										upgradeSoft
												.setCurrentVersion(packageInfo.versionName);
										upgradeSoft.setNewVersion(soft
												.getVersion());
									}
									dao.insert(upgradeSoft);

									if (now == all) {
										System.out.println("now=" + now
												+ ",all=" + all);
										System.out.println("有" + newVersion
												+ "个应用可更新");
									}
								}
							}, new ErrorListener() {

								@Override
								public void onErrorResponse(VolleyError error) {
									now += 1;
									System.out.println("now=" + now);

									upgradeSoft.setSoftId(null);
									upgradeSoft.setSize(0);
									upgradeSoft.setNewVersion(null);
									upgradeSoft.setId(UUID.randomUUID()
											.toString());
									upgradeSoft
											.setName(packageInfo.applicationInfo
													.loadLabel(
															context.getPackageManager())
													.toString());
									upgradeSoft
											.setPackageName(packageInfo.packageName);
									upgradeSoft
											.setCurrentVersion(packageInfo.versionName);
									dao.insert(upgradeSoft);
									if (now == all) {
										System.out.println("now=" + now
												+ ",all=" + all);
										System.out.println("有" + newVersion
												+ "个应用可更新");
									}
								}
							});
					mQueue.add(jsonObjectRequest);
				} else {
					UpgradeSoft currentSoft = dao
							.query(packageInfo.packageName);
					if (currentSoft.getNewVersion() != null
							&& currentSoft.getNewVersion().equals(
									packageInfo.versionName)) {
						dao.update(packageInfo.packageName);
					}

					if (i == packages.size()) {
						if (now == all) {
							System.out.println("now=" + now + ",all=" + all);
							System.out.println("有" + newVersion + "个应用可更新");
						}
					}
				}
			}

		}

	}
}
